#include "stm32f10x.h"                  // Device header

void PWN_Init_TIM1()
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 | RCC_APB2Periph_GPIOA,ENABLE);
	//开启io口
	GPIO_InitTypeDef GPIO_InitStruct;                         
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;							//复用输出
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;      //开启PA8，9，10，11
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;            //速度随便
	GPIO_Init(GPIOA,&GPIO_InitStruct);
	
	//TIM_InternalClockConfig(TIM1);
	//配置TIM1的时基单元
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;				//不分频
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;    //向上计数
	TIM_TimeBaseInitStruct.TIM_Period = 7200 - 1;                   //自动重装值(arr)为7200
	TIM_TimeBaseInitStruct.TIM_Prescaler = 0;                       //不预分频
	TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0x00;            //不用重复计数器
	//写入TIM1的时基单元
	TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStruct);
	//配置TIM1的通道1，2，3，4的输出比较  输出PWM波
	TIM_OCInitTypeDef TIM_OCInitStruct;
	TIM_OCStructInit(&TIM_OCInitStruct);
	TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;									//PWM模式一
	TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;          //不反相
	TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;      //使能输出
	TIM_OCInitStruct.TIM_Pulse = 2000;
	
	TIM_OC1Init(TIM1,&TIM_OCInitStruct);
	TIM_OC3Init(TIM1,&TIM_OCInitStruct);
	
	TIM_OCInitStruct.TIM_Pulse = 0;
	
	TIM_OC2Init(TIM1,&TIM_OCInitStruct);
	TIM_OC4Init(TIM1,&TIM_OCInitStruct);
	
	TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable);				//使能TIM1的通道1(CCR)的预装载
	TIM_OC2PreloadConfig(TIM1,TIM_OCPreload_Enable);				//使能TIM1的通道2(CCR)的预装载
	TIM_OC3PreloadConfig(TIM1,TIM_OCPreload_Enable);				//使能TIM1的通道3(CCR)的预装载
	TIM_OC4PreloadConfig(TIM1,TIM_OCPreload_Enable);        //使能TIM1的通道4(CCR)的预装载
	TIM_ARRPreloadConfig(TIM1,ENABLE);                      //使能TIM1的通道1(ARR)的预装载
	
	TIM_CtrlPWMOutputs(TIM1,ENABLE);												//使能TIM1的PWM输出
	
	TIM_Cmd(TIM1,ENABLE);
}
